package micro86.engine;

import micro86.operation.IOperation;
import micro86.operation.IOperationException;

import java.util.List;

/**
 * (c) Kate Luzhevskaya 2013
 */
public class Processor {
    State state;
    Memory memory;
    List<IOperation> operationList;

    public Processor (State state, Memory memory, List<IOperation> operationList) {
        this.state = state;
        this.memory = memory;
        this.operationList = operationList;
    }

    public void run () {
        State state = this.state;
        List<IOperation> operationList = this.operationList;
        IOperation operation;
        int length = operationList.size();
        int index = 0;

        state.setIP(0);
        while (state.getIP() <= length -1) {
            try {
                index = state.getIP();
                operation = operationList.get(index);
                operation.execute(state, memory);
            } catch (IOperationException e) {
                System.out.print("\nError in line " + (index + 1) + " : " + e);
                break;
            }
        }
    }
}

